package com.joesem.sale.client.logic.workOrder.option

import android.os.Bundle
import android.util.Log
import android.view.inputmethod.EditorInfo
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import com.blankj.utilcode.util.KeyboardUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.jiaxing.sale.client.R
import com.jiaxing.sale.client.databinding.FragmentCustomerListBinding
import com.joesem.sale.client.base.BaseFragment
import com.joesem.sale.client.base.color
import com.joesem.sale.client.base.createViewModel
import com.joesem.sale.client.logic.workOrder.option.viewModel.OptionViewModel
import com.joesem.sale.client.network.data.CustomerListMode
import com.joesem.sale.client.utils.click

class CustomerListDialog : BaseFragment<FragmentCustomerListBinding>() {
    companion object {
        var customerSelected: (CustomerListMode) -> Unit = {}
    }

    private val listAdapter =
        object : BaseQuickAdapter<CustomerListMode, BaseViewHolder>(R.layout.item_customer) {
            override fun convert(holder: BaseViewHolder, item: CustomerListMode) {
                holder.setText(R.id.tvCustomerName, item.name.orEmpty())
                holder.setText(R.id.tvCustomerTelephone, "联系电话：${item.user?.phone.orEmpty()}")
                holder.setText(R.id.tvCustomerUsername, "联系人：${item.user?.name.orEmpty()}")
                holder.setText(R.id.tvCustomerUserAddress, item.user?.address.orEmpty())
                holder.itemView.click {
                    customerSelected.invoke(item)
                    requireActivity().onBackPressed()
                }
            }
        }
    private val viewMode by createViewModel<OptionViewModel>()

    override fun FragmentCustomerListBinding.initView(savedInstanceState: Bundle?) {
        rvCustomerList.adapter = listAdapter
    }

    override fun FragmentCustomerListBinding.initListener() {
        root.click { }
        vTop.click { requireActivity().onBackPressed() }
        btnClose.click {
            val keyword = etSearchAction.text.toString()
            if (keyword.isEmpty())
                requireActivity().onBackPressed()
            else
                etSearchAction.setText("")
        }
        lifecycle.addObserver(object : DefaultLifecycleObserver {
            override fun onCreate(owner: LifecycleOwner) {
                srlListRefresh.autoRefresh()
            }

            override fun onResume(owner: LifecycleOwner) {
                showAnimation(bind.root)
            }

            override fun onStop(owner: LifecycleOwner) {
                root.setBackgroundColor(R.color.white.color())
            }
        })
        srlListRefresh.apply {

            setOnRefreshListener {
                val keyword = etSearchAction.text.toString()
                viewMode.getCustomerList(keyword = keyword, isFirstPage = true)
            }
            setOnLoadMoreListener {
                val keyword = etSearchAction.text.toString()
                viewMode.getCustomerList(keyword = keyword, isFirstPage = false)
            }
        }
        etSearchAction.setOnEditorActionListener { v, actionId, _ ->
            when (actionId) {
                EditorInfo.IME_ACTION_SEARCH -> {
                    srlListRefresh.autoRefresh()
                    KeyboardUtils.hideSoftInput(v)
                }
            }
            true
        }
    }

    override fun FragmentCustomerListBinding.initIntent(intent: Any?) {
        when (intent) {
            is OptionViewModel.ListIntent -> {
                srlListRefresh.finishRefresh()
                srlListRefresh.finishLoadMore()
                if (intent.isFirst) {
                    listAdapter.setList(intent.data)
                } else {
                    listAdapter.addData(intent.data)
                }
            }
        }
    }
}


fun optionCustomer(
    contentViewId: Int,
    fm: FragmentManager,
    customerSelected: (CustomerListMode) -> Unit
) {
    CustomerListDialog.customerSelected = customerSelected
    fm.beginTransaction().setCustomAnimations(
        R.anim.slide_in_bottom,
        R.anim.slide_out_bottom,
        R.anim.slide_in_bottom,
        R.anim.slide_out_bottom,
    ).replace(
        contentViewId,
        BaseFragment.newInstance<CustomerListDialog>(Bundle())
    ).addToBackStack(null).commit()
}

